/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.location;

import java.util.Random;

import com.aroundj2me.controller.ApplicationController;
import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.model.Location;

/**
 * Simulates changing locations
 *
 */
public class LocationController {
	private ApplicationController parent;	
	private LocationView locationView = null;
	private int currentLocation = 0;
	
	//set of dummy locations
	private Location[] locations = 
		{
			new Location("1", "20.5", "13.5"),
			new Location("2", "15.5", "100.5"),
			new Location("3", "10.5", "58.5"),
			new Location("4", "30.5", "5.5"),
			new Location("5", "46.4982135", "11.354706")
		};
	
	public LocationController(ApplicationController firstlife){
		this.parent = firstlife;
		currentLocation = generateRandom();
	}
	
	/**
	 * Generates a random int from 0 to the length of the
	 * location-array
	 * @return
	 */
	private int generateRandom(){
		Random r = new Random();
		int x = r.nextInt(locations.length);
		//enforce that different is chosen
		while(x==currentLocation){
			x = r.nextInt(locations.length);
		}
		return x;
	}
	
	/**
	 * retrieves a location randomly
	 * @return
	 */
	public Location getRandomLocation(){
		currentLocation = generateRandom();
		return locations[currentLocation];
	}
	
	/**
	 * Retrieves the next Location object of the
	 * array
	 * @return
	 */
	public Location getNextLocation(){
		currentLocation++;
		if(currentLocation>locations.length-1)
			currentLocation = 0;
		return locations[currentLocation];
	}
	
	/**
	 * Gets the current location
	 * @return
	 */
	public Location getCurrentLocation(){
		return locations[currentLocation];
	}
	
	public void doBackCommand(){
		locationView = null;
		parent.loadInitialNavigationList();
	}
	
	/**
	 * Shows the view with the details of the
	 * current location
	 *
	 */
	public void showView(){
		if(locationView == null) locationView = new LocationView(this);
		locationView.initialize(getCurrentLocation());
		ScreenController.getInstance().display(locationView, true);
	}

	/**
	 * Switches the current location by getting a new random one
	 * and displaying it on the view
	 *
	 */
	public void doRandomLocation() {
		locationView.initialize(getRandomLocation());
	}

	public void doNextLocation() {
		locationView.initialize(getNextLocation());
	}
}